﻿/*
____
 L|ypps Web controls for ASP.NET

Copyright (c) 2009 Alessandro Zifiglio, http://www.Typps.com
EMail: alessandro@Typps.com

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
namespace Typps
{
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.Design;
    using System.Drawing.Design;
    using System.Runtime.Serialization;
    using System.Text;
    using System.Web.UI;

    //or add each known type one after the other here.
    [CollectionDataContract]
    [KnownType("KnownTypes")]
    public class EffectsCollection : StateManagedCollection
    {
        #region Fields

        private static readonly Type[] knownTypes;

        #endregion Fields

        #region Constructors

        static EffectsCollection()
        {
            EffectsCollection.knownTypes = KnownTypes();
        }

        #endregion Constructors

        #region Indexers

        public BaseEffect this[int index]
        {
            get
            {
                return (BaseEffect)((IList)this)[index];
            }
            set
            {
                ((IList)this)[index] = value;
            }
        }

        #endregion Indexers

        #region Methods

        public static Type[] KnownTypes()
        {
            return new Type[] {
                              typeof(Blind),
                              typeof(Fade),
                              typeof(Grow),
                              typeof(Highlight),
                              typeof(Shake),
                              typeof(Slide),
                              typeof(Squish),
                              typeof(ColorEffect),
                              typeof(Move),
                              typeof(Size),
                              typeof(Fold),
                              typeof(DropOut),
                              typeof(Puff),
                              typeof(Pulsate)
                            };
        }

        // Restricts to BaseEffect types, items that can be added to the collection
        public int Add(BaseEffect value)
        {
            return ((IList)this).Add(value);
        }

        public bool Contains(BaseEffect value)
        {
            // If value is not of type BaseEffect, this will return false.
            return ((IList)this).Contains(value);
        }

        public void CopyTo(BaseEffect[] toolBarSetArray, int index)
        {
            base.CopyTo(toolBarSetArray, index);
        }

        public int IndexOf(BaseEffect value)
        {
            return ((IList)this).IndexOf(value);
        }

        public void Insert(int index, BaseEffect value)
        {
            ((IList)this).Insert(index, value);
        }

        public void Remove(BaseEffect value)
        {
            ((IList)this).Remove(value);
        }

        public void RemoveAt(int index)
        {
            ((IList)this).RemoveAt(index);
        }

        protected override object CreateKnownType(int index)
        {
            switch (index)
            {
                case 0:
                    return new Blind();
                case 1:
                    return new Fade();
                case 2:
                    return new Grow();
                case 3:
                    return new Highlight();
                case 4:
                    return new Shake();
                case 5:
                    return new Slide();
                case 6:
                    return new Squish();
                case 7:
                    return new ColorEffect();
                case 8:
                    return new Move();
                case 9:
                    return new Size();
                case 10:
                    return new Fold();
                case 11:
                    return new DropOut();
                case 12:
                    return new Puff();
                case 13:
                    return new Pulsate();
                default:
                    throw new ArgumentOutOfRangeException("Unknown type in viewstate. Did you forget to provide a known type in knownTypes array?");
            }
        }

        protected override Type[] GetKnownTypes()
        {
            return EffectsCollection.knownTypes;
        }

        protected override void OnClear()
        {
            base.OnClear();
        }

        protected override void OnInsert(int index, object value)
        {
            base.OnInsert(index, value);
            if (!(value is BaseEffect))
            {
                throw new ArgumentException("value must be of type BaseEffect.", "value");
            }
        }

        protected override void OnValidate(Object value)
        {
            if (!(value is BaseEffect))
                throw new ArgumentException("value must be of type BaseEffect.", "value");
        }

        protected override void SetDirtyObject(object o)
        {
            if (o is BaseEffect)
            {
                ((BaseEffect)o).SetDirty();
            }
        }

        #endregion Methods
    }
}